package com.aynu.gulimall.order.listener;

import com.aynu.gulimall.order.entity.OrderEntity;
import com.aynu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author mj
 * @version 1.0
 * @date 2021/4/10 22:37
 */
@RabbitListener(queues = "order.release.order.queue")
@Slf4j
@Service
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void closeListener(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        log.info("收到过期的订单信息：订单:{},已创建30分钟，检查订单是否需要关单",orderEntity.getOrderSn());
        try {
            orderService.closeOrder(orderEntity);
            //手动收单

            //手动应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            e.printStackTrace();
            //关单出现异常，拒接接受信息，将消息重新放入消息队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
